﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace SBLib.Common.FileKS
{
    class FileFuncKS
    {
        /// <summary>
        /// Reading a file in a string
        /// </summary>
        /// <param name="prmPath">File Path</param>
        /// <returns></returns>
        public static string ReadATextFile(string prmPath)
        {
            TextReader txtReader = new StreamReader(prmPath);
            try
            {
                try
                {
                    if (txtReader != null)
                    {
                        return txtReader.ReadToEnd();
                    }

                    return "";
                }
                catch (Exception)
                {

                    return "";
                }
                finally
                {
                    if (txtReader != null)
                    {
                        txtReader.Close();
                        txtReader.Dispose();
                    }
                }

            }
            catch (Exception)
            {
                txtReader = null;
                return "";
            }
        }

        public static string GetFilePathFromRelativePhysical(string prmPath)
        {
            try
            {
                if (string.IsNullOrEmpty(prmPath) || prmPath.Trim().Length == 0)
                {
                    return "";
                }
                if (prmPath.StartsWith("~"))
                {
                    return System.Web.HttpContext.Current.Server.MapPath(prmPath);
                }
                else
                {
                    return String.Format(prmPath);
                }
            }
            catch (Exception)
            {

                return "";
            }
        }

        public static string GetFilePathFromRelativePhysical(string prmPath, string prmFileName)
        {
            try
            {
                if (string.IsNullOrEmpty(prmPath) || prmPath.Trim().Length == 0)
                {
                    return "";
                }
                if (prmPath.StartsWith("~"))
                {
                    return Path.Combine(System.Web.HttpContext.Current.Server.MapPath(prmPath), prmFileName);
                }
                else
                {
                    return String.Format(prmPath);
                }
            }
            catch (Exception)
            {

                return "";
            }
        }
    }
}
